<template>
  <div style="width:100%; height: 100vh; background-color: #cccccc; overflow: hidden">
    <div style="width: 400px; margin: 150px auto">
      <h1 style="text-align: center">共享存储柜管理系统</h1>
      <div style=" font-size: 30px; text-align: center; padding: 30px 0">欢迎登陆</div>
      <el-form :model="form" :rules="rules" ref="form" >
        <el-form-item>
          <el-input v-model="form.username" placeholder="请输入用户名" :suffix-icon="Calendar"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.password" placeholder="请输入密码" show-password clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="toRegister">没有账号？点我注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" style="width: 100%" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import request from "../utils/request";
import {Calendar} from '@element-plus/icons-vue';

export default {
  name: "Login",
  data() {
    return {
      form:{
        username:"",
        password:""
      },
      rules: {
        username:[{required: true, message:"请输入用户名", trigger: "blur"} ],
        password:[{required: true, message:"请输入密码", trigger: "blur"} ],
      }
    }
  },
  methods:{
    toRegister(){
      this.$router.replace("/register")
    },
    login(){
      this.$refs["form"].validate((valid) =>{
        if (valid){
          request.post("/user/login",this.form).then(res =>{
            if (res.data.code === "0"){
              this.$message({
                type: "success",
                message: "登录成功"
              })
              console.log(res.data.data)
              // 缓存用户信息
              sessionStorage.setItem("user",JSON.stringify(res.data.data))
              // sessionStorage.setItem("user",res.data.data)
              // console.log(sessionStorage.getItem("user",JSON.stringify(res.data.data)))
              // 登录成功后跳转页面
              this.$router.push("/")
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
